import { defineStore } from 'pinia'
import { store } from '@/store'
import { CACHE_KEY } from '@/hooks/web/useCache'
import { LocaleDropdownType } from '@/types/localeDropdown'
import { LocaleState } from './types/locale'

const { wsCache } = useCache()

export const useLocaleStore = defineStore('locales', {
  state: (): LocaleState => {
    const localLocale: LocaleDropdownType | null = wsCache.get(CACHE_KEY.LANG)
    return {
      currentLocale: localLocale || { lang: 'zh-CN', name: '简体中文' },
      // 多语言
      localeMap: [
        {
          lang: 'zh-CN',
          name: '简体中文'
        },
        {
          lang: 'en',
          name: 'English'
        }
      ]
    }
  },
  getters: {
    getCurrentLocale(): LocaleDropdownType {
      return this.currentLocale
    },
    getLocaleMap(): LocaleDropdownType[] {
      return this.localeMap
    }
  },
  actions: {
    setCurrentLocale(localeMap: LocaleDropdownType) {
      this.currentLocale.lang = localeMap.lang
      this.currentLocale.name = localeMap.name
      wsCache.set(CACHE_KEY.LANG, localeMap)
    }
  }
})

export const useLocaleStoreWithOut = () => {
  return useLocaleStore(store)
}
